<!--
 * @Author: your name
 * @Date: 2021-04-09 14:08:43
 * @LastEditTime: 2021-04-30 11:55:25
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\views\device\lease\option.vue
-->
<template>
    <div class="option-lease-main-box">
        <el-dialog
        :title="$t('lease.option')"
        :visible="visible"
        width="700px"
        @close="$emit('update:visible', false);$emit('update:editData', {})"
        :close-on-click-modal='false'
        >
            <div>
                <baseForm :option='option' v-model="form" ref='child'>
                    <template #nice='{data}'>
                        {{editData.SN}}
                    </template>
                    <template #nice2='{data}'>
                        <el-switch
                        v-model="data.form[data.item.name]"
                        active-color="#fea429"
                        inactive-color="#ccc"
                        @change='changeLeaseMode'
                        :active-text="$t('lease.On')"
                        :inactive-text="$t('lease.Off')"
                        >
                        </el-switch>
                    </template>
                    <template #nice3='{data}'>
                        <el-switch
                        v-model="data.form[data.item.name]"
                        active-color="#fea429"
                        inactive-color="#ccc"
                        @change='changeLease(152)'
                        :active-text="$t('lease.Locked')"
                        :inactive-text="$t('lease.Unlocked')"
                        :disabled="disabledFlag"
                        >
                        </el-switch>
                    </template>
                </baseForm>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="$emit('update:visible', false);$emit('update:editData', {})">{{$t('cancel')}}</el-button>
                <el-button :loading="loading"  type="primary" @click="save('form')">{{$t('sure')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import baseForm from '../../../components/common/baseForm'
export default {
    components: {
        baseForm
    },
    props: {
        visible:{
            type:Boolean,
            default:false
        },
        editData:{
            type:Object,
            default:()=> {return {}}
        }
    },
    data() {
        return {
            form:{},
            tokenid:'',
            leaseMode:'',
            lockedStatus:'',
            loading:false,
            disabledFlag:true,
            set1:'',
            set2:'',
            get1:'',
            get2:'',
        };
    },
    computed: {
        option(){
            return {
                columnNum:'1',
                data:[{
                    type:'slot',
                    slot:'nice',
                    lable:'SN',
                    name:'slot',
                    rules:[{ required: false, message: '不能为空'}]
                }
                ,{
                    type:'slot',
                    slot:'nice2',
                    lable:this.$t('lease.type'),
                    name:'slot2',
                    value:this.leaseMode?true:false,
                    rules:[{ required: false, message: '不能为空'}],
                    watch:this.changeLeaseMode
                },{
                    type:'slot',
                    slot:'nice3',
                    lable:this.$t('lease.bindStatus'),
                    name:'slot3',
                    value:this.lockedStatus?true:false,
                    rules:[{ required: false, message: '不能为空'}]
                }
               ],
               
            }}
    },
    watch: {
        visible:{
            handler(nv,ov){
                if(nv){
                    this.getDictionaryBatch()
                }else{
                    this.leaseMode = ''
                    this.lockedStatus = ''
                }
            }
        },
        editData:{
            handler(nv,ov){
                if(nv){
                    
                }
            }
        }
    },
    created() {

    },
    mounted() {
    },
    methods: {
        changeLeaseMode(val){
            // console.log(val,'val');
            if(!val){
                this.lockedStatus = false
                this.leaseMode = false
                this.disabledFlag  = true
            }else{
                this.leaseMode = true
                this.disabledFlag  = false
            }
        },
        async getDictionaryBatch(){
            this.loading = true
            let res = await this.API.dictionaryGetDictionary({
                dictCode:'LEASEMODE_SETTING',
                code:this.editData.inverterType,
                lang:'zh_CN'
            })
            this.textDataList = res || {}
            this.loading = false
            this.getTokenId()
            if(res){
                res.forEach(item =>{
                    if(item.code == this.editData.inverterType){
                        this.set1 = item.value.split('_')[0]
                        this.set2 = item.value.split('_')[1]
                        this.get1 = item.remark.split('_')[0]
                        this.get2 = item.remark.split('_')[1]
                    }
                })
            }
        },
        async settingnewParamInit(){
            this.loading = true
            let res = await this.API.settingnewParamInit({
                tokenId:this.tokenid,
                sn:this.editData.wifiSN
            })
            if(res.success){
                this.leaseMode = res.result[this.get1-1] 
                this.disabledFlag = this.leaseMode?false:true
                this.lockedStatus = res.result[this.get2-1] 
            }else{
                this.comm.showMsg(false,this.$t('API.gete'))
            }
            this.loading = false
        },
        async getTokenId(){
            let loclData = JSON.parse(localStorage.getItem('appUserObject'))
            let id = localStorage.getItem('appUserId')
            let res = await this.API.loginRemoteLogin({
                sn: this.editData.SN,
                userType: loclData.userType,
                userId: id,
                firmId: loclData.relIds
            })
            if(res.success){
                this.tokenid= res.result.tokenId
                this.settingnewParamInit()
            }
            
        },
        async changeLease(reg){
            
            
        },
        async save(){
            if(!this.$refs.child.save()){
                this.comm.showMsg(false)
                return
            }
            this.loading = true
            let res = await this.API.settingnewParamSet({
                optType: 'setReg',
                num:'2',
                sn:this.editData.wifiSN,
                tokenId:this.tokenid,
                Data:JSON.stringify([{reg:this.set1,val:this.form.slot2 ? 1:0},{reg:this.set2,val:this.form.slot3 ? 1:0}]),
                inverterSn:this.editData.SN,
                leaseMode:this.form.slot2?'1':'0',
                lockedStatus:this.form.slot3?'1':'0',
            })
            this.loading = false
            if(this.comm.showMsg(res.success)){
                this.$emit('update:visible', false)
                this.$emit('uploadData',true)
            }
        }
    },
};
</script>

<style  lang="less" scoped>

</style>
